/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmBao.java
 *
 * Created on May 21, 2011, 10:43:49 PM
 */

package Presentation;

import RSCall.GiaTienQCToBuomRSc;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author VooKa
 */
public class frmGiaTienToBuom extends javax.swing.JInternalFrame {

    /** Creates new form frmBao */
    public frmGiaTienToBuom() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tbxSoLuong = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblGiaToBuom = new javax.swing.JTable();
        tbxGiaTien = new javax.swing.JTextField();

        setTitle(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("titlefrmGiaTienToBuom"));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel1.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("quantity"));

        tbxSoLuong.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxSoLuongKeyTyped(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel2.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("cost"));

        btnAdd.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnAdd.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("insert"));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnUpdate.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnUpdate.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("update"));
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnDelete.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("delete"));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        tblGiaToBuom.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblGiaToBuom.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblGiaToBuomMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblGiaToBuom);

        tbxGiaTien.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxGiaTienKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(44, 44, 44)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(tbxGiaTien)
                                    .addComponent(tbxSoLuong, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tbxSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tbxGiaTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnUpdate)
                    .addComponent(btnDelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        this.LoadData();
    }//GEN-LAST:event_formInternalFrameOpened

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        String soLuong = tbxSoLuong.getText().trim();
        String giaTien = tbxGiaTien.getText().trim();
        
        if(soLuong.isEmpty()){
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("chuanhapsoluong"));
        }else if(giaTien.isEmpty()){
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("chuanhapgiatien"));
        }else{
            Float giaTienToBuom = Float.MIN_VALUE;
            int soLuongToBuom = 0;
            try{
                RSCall.GiaTienQCToBuomRSc giaToBuom = new GiaTienQCToBuomRSc();
                soLuongToBuom = Integer.parseInt(soLuong);
                giaTienToBuom = Float.parseFloat(giaTien);
                giaToBuom.Insert(soLuongToBuom, giaTienToBuom);                
                this.LoadData();
                tbxSoLuong.setText("");
                tbxGiaTien.setText("");
                JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("insertarecord"));
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(this, e.toString());
            }
        }

    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        String soLuong = tbxSoLuong.getText().trim();
        String giaTien = tbxGiaTien.getText().trim();
        if(this.tblGiaToBuom.getSelectedRow()==-1){
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("mustselectGiaTien"));
        }if(soLuong.isEmpty()){
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("chuanhapsoluong"));
        }else if(giaTien.isEmpty()){
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("notentercost"));
        }else{
            Float giaTienToBuom = Float.MIN_VALUE;
            int soLuongToBuom = 0;
            try{
                RSCall.GiaTienQCToBuomRSc giaToBuom = new GiaTienQCToBuomRSc();
                soLuongToBuom = Integer.parseInt(soLuong);
                giaTienToBuom = Float.parseFloat(giaTien);
                String maGiaTien = tblGiaToBuom.getValueAt(tblGiaToBuom.getSelectedRow(), 1).toString();
                giaToBuom.Update(maGiaTien, soLuongToBuom, giaTienToBuom);
                this.LoadData();
                tbxSoLuong.setText("");
                tbxGiaTien.setText("");
                JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("updatedToBuom"));
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(this, e.toString());
            }
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void tblGiaToBuomMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblGiaToBuomMouseClicked
        // TODO add your handling code here:
        try
        {
            int n = this.tblGiaToBuom.getSelectedRow();
            tbxSoLuong.setText(this.tblGiaToBuom.getValueAt(n, 2).toString());
            tbxGiaTien.setText(this.tblGiaToBuom.getValueAt(n, 3).toString());
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }//GEN-LAST:event_tblGiaToBuomMouseClicked

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if(this.tblGiaToBuom.getSelectedRow()==-1)
        {
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("mustselectGiaTien"));
        }
        else
        {
            int n = JOptionPane.showConfirmDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("really delete"),java.util.ResourceBundle.getBundle("Presentation/vn").getString("warn"),JOptionPane.YES_NO_OPTION);
            if(n==0)
            {
                try
                {
                    RSCall.GiaTienQCToBuomRSc giaToBuom = new GiaTienQCToBuomRSc();
                    giaToBuom.Delete((String)this.tblGiaToBuom.getValueAt(this.tblGiaToBuom.getSelectedRow(), 1));
                    this.LoadData();
                    tbxSoLuong.setText("");
                    JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("deleted"));
                }
                catch(Exception e)
                {
                    JOptionPane.showMessageDialog(this,e.toString());
                }
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void tbxSoLuongKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxSoLuongKeyTyped
        Utilities.Utilities.tbxKeyTyped(evt);
    }//GEN-LAST:event_tbxSoLuongKeyTyped

    private void tbxGiaTienKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxGiaTienKeyTyped
        Utilities.Utilities.tbxKeyTyped(evt);
    }//GEN-LAST:event_tbxGiaTienKeyTyped
    private void LoadData(){
        try
        {
            RSCall.GiaTienQCToBuomRSc toBuom = new GiaTienQCToBuomRSc();
            ArrayList<Entities.GIATIEN_TOBUOM> lst = toBuom.GetAllRows();
            DefaultTableModel dtm = new DefaultTableModel(){
                @Override
                public boolean isCellEditable(int x,int y){
                    return false;
                }
            };
            dtm.addColumn("STT");
            dtm.addColumn("Mã Giá tiền");
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("quantity"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("cost"));
            String s = "";
            for(int i=0;i<lst.size();i++)
            {
                Vector _v = new Vector();
                Entities.GIATIEN_TOBUOM _b = lst.get(i);
                _v.add(i+1);
                _v.add(_b.getMaGiaTien());
                _v.add(_b.getSoLuong());
                _v.add(_b.getGiaTien());
                dtm.addRow(_v);
            }
            this.tblGiaToBuom.setModel(dtm);
            this.tblGiaToBuom.getColumnModel().getColumn(0).setPreferredWidth(10);
            this.tblGiaToBuom.getColumnModel().getColumn(1).setPreferredWidth(30);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblGiaToBuom;
    private javax.swing.JTextField tbxGiaTien;
    private javax.swing.JTextField tbxSoLuong;
    // End of variables declaration//GEN-END:variables
}